package org.zachary.agent.application;

import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.zachary.agent.domain.aggregate_model.user.User;
import org.zachary.agent.domain.aggregate_model.user_credit.UserCredit;
import org.zachary.agent.domain.repository.CheckUcsInfoRepository;
import org.zachary.agent.domain.repository.UserCreditRepository;
import org.zachary.agent.domain.repository.UserRepository;
import org.zachary.agent.infrastructure.repository.InitDimensionAndRankingTableMapper;
import org.zachary.agent.infrastructure.utils.CreditScoreCalculator;
import org.zachary.agent.infrastructure.utils.UUIdUtil;
import org.zachary.commonmodule.entity.MqTransmissionJson;

import java.time.Instant;
import java.util.Map;

@Slf4j
@Service
public class UserCreditService {
    private final UserCreditRepository userCreditRepository;
    private final UserRepository userRepository;
    private final KafkaProducerService kafkaProducerService;
    private static final Integer ServiceType_BasicInfo=0;
    private final InitDimensionAndRankingTableMapper mapper;
    private final CheckUcsInfoRepository checkUcsInfoRepository;

    public UserCreditService(UserCreditRepository userCreditRepository, UserRepository userRepository, KafkaProducerService kafkaProducerService, InitDimensionAndRankingTableMapper mapper, CheckUcsInfoRepository checkUcsInfoRepository) {
        this.userCreditRepository = userCreditRepository;
        this.userRepository = userRepository;
        this.kafkaProducerService = kafkaProducerService;
        this.mapper = mapper;
        this.checkUcsInfoRepository = checkUcsInfoRepository;

    }

    @Transactional
    public int updateInfoAndCalculateCreditScore(Integer id, UserCredit userCredit) {
        User u=userRepository.findById(id);
        Map<String, Object> map = CreditScoreCalculator.calculateCreditScore(u.getUsername(),userCredit);
        System.out.println(map);
        if (map.containsKey("error")) {
            throw new RuntimeException("Error: " + map.get("error"));
        } else {
            log.info("用户ID: " + map.get("userId")+"---信用分："+map.get("creditScore"));
        }
        int score = 660;//(int) map.get("creditScore");

        userCredit.setCreditScore(score,userCredit);

        if(score<=100 || score>1100){
            throw new RuntimeException("信用分计算有误");
        }
        MqTransmissionJson json=new MqTransmissionJson(
                UUIdUtil.createUUId(),
                id,
                ServiceType_BasicInfo,
                false,
                0,
                false,
                0.00,
                "用户基础信用分计算",
                Instant.now(),
                Instant.now()
        );
        kafkaProducerService.sendJsonMessage(json);


        userCreditRepository.update(id, userCredit);
        return score;
    }
    @Transactional
    public UserCredit findById(Integer id) {
        User u=userRepository.findById(id);
        if(u==null){
            return null;
        }
        UserCredit uc= userCreditRepository.findById(id).orElse(null);
        Integer score=mapper.getUserCreditScore(id);
        assert uc != null;
        uc.setCreditScore(score,uc);

        System.out.println(uc.getCreditScore());
        return uc;
    }
    @Transactional
    public void notPass(Integer checkerId,Integer userId) {
        checkUcsInfoRepository.setChecker(checkerId,userId);
        userCreditRepository.setInfoNotPassed(userId);

    }
    @Transactional
    public void pass(Integer checkerId,Integer userId) {
        checkUcsInfoRepository.setChecker(checkerId,userId);
        userCreditRepository.setInfoPassed(userId);
    }

    @Transactional
    public void setInImproving(Integer id) {
        checkUcsInfoRepository.save(id);
        userCreditRepository.setInfoInImprovement(id);

    }
}
